package com.zbkj.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.model.shop.*;
import com.zbkj.common.model.user.User;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.EbGroupPurchaseRequest;
import com.zbkj.common.response.EbGroupPurchaseResponse;
import com.zbkj.common.response.EbGroupPurchaseStatisticsVo;
import com.zbkj.common.result.CommonResultCode;
import com.zbkj.service.dao.EbGroupPurchaseBusinessHoursDao;
import com.zbkj.service.dao.EbGroupPurchaseDao;
import com.zbkj.service.dao.EbGroupPurchaseGoodsDao;
import com.zbkj.service.dao.EbGroupPurchaseShopDao;
import com.zbkj.service.dao.shop.EbGroupBuyShopDao;
import com.zbkj.service.service.EbGroupPurchaseService;
import com.zbkj.service.service.EbIndustryCategoryService;
import com.zbkj.service.service.MerchantService;
import com.zbkj.service.service.UserService;
import com.zbkj.service.util.ListUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class EbGroupPurchaseServiceImpl extends ServiceImpl<EbGroupPurchaseDao, EbGroupPurchase> implements EbGroupPurchaseService {
    @Resource
    EbGroupPurchaseDao ebGroupPurchaseDao;
    @Resource
    EbGroupPurchaseBusinessHoursDao ebGroupPurchaseBusinessHoursDao;
    @Resource
    EbGroupPurchaseGoodsDao ebGroupPurchaseGoodsDao;
    @Resource
    EbGroupPurchaseShopDao ebGroupPurchaseShopDao;
    @Resource
    EbGroupBuyShopDao ebGroupBuyShopDao;
    @Resource
    EbIndustryCategoryService ebIndustryCategoryService;
    @Autowired
    UserService userService;
    @Autowired
    MerchantService merchantService;

    /**
     * 增加团购
     *
     * @param request
     * @return
     */
    @Override
    public EbGroupPurchase add(EbGroupPurchaseRequest request) {
        log.info("add:{}", request.toString());
        Date date = new Date();
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        EbGroupPurchase ebGroupPurchase = new EbGroupPurchase();
        BeanUtils.copyProperties(request, ebGroupPurchase);
        ebGroupPurchase.setStatus(false);
        ebGroupPurchase.setCheckStatus(0);
        ebGroupPurchase.setIsDelete(0);
        ebGroupPurchase.setMerId(merchant.getId());
        ebGroupPurchase.setMerName(merchant.getName());
        ebGroupPurchase.setCreateTime(date);
        ebGroupPurchase.setUpdateTime(date);
        if (request.getBannerImageList() != null) {
            ebGroupPurchase.setBannerImages(ListUtils.getListStr(request.getBannerImageList()));
        }
        if (request.getDetailImageList() != null) {
            ebGroupPurchase.setDetailImages(ListUtils.getListStr(request.getDetailImageList()));
        }
        Integer sortNum = (Integer) ebGroupPurchaseDao.selectObjs(new LambdaQueryWrapper<EbGroupPurchase>().select(EbGroupPurchase::getSortNum).eq(EbGroupPurchase::getMerId, ebGroupPurchase.getMerId()).orderByDesc(EbGroupPurchase::getSortNum).last("LIMIT 1")).stream().findFirst().orElse(0);

        ebGroupPurchase.setSortNum(sortNum + 1);
        ebGroupPurchaseDao.insert(ebGroupPurchase);
        if (CollectionUtils.isNotEmpty(request.getGroupPurchaseBusinessHoursList())) {
            //团购可用时间
            for (EbGroupPurchaseBusinessHours hours : request.getGroupPurchaseBusinessHoursList()) {
                hours.setEbGroupPurchaseId(ebGroupPurchase.getId());
                ebGroupPurchaseBusinessHoursDao.insert(hours);
            }
        }

        if (CollectionUtils.isNotEmpty(request.getGoodsList())) {
            //团购商品
            for (EbGroupPurchaseGoods ebGroupPurchaseGoods : request.getGoodsList()) {
                ebGroupPurchaseGoods.setEbGroupPurchaseId(ebGroupPurchase.getId());
                ebGroupPurchaseGoodsDao.insert(ebGroupPurchaseGoods);
            }
        }
        StringBuffer sbf = new StringBuffer();
        if (request.getIsAllShop() != null && request.getIsAllShop() == 1) {
            List<EbGroupBuyShop> merShopList = ebGroupBuyShopDao.selectList(new LambdaQueryWrapper<EbGroupBuyShop>().eq(EbGroupBuyShop::getMerId, merchant.getId()));
            List<EbGroupPurchaseShop> groupPurchaseShopList = new ArrayList<>();
            for (EbGroupBuyShop ebGroupBuyShop : merShopList) {
                EbGroupPurchaseShop ebGroupPurchaseShop = new EbGroupPurchaseShop();
                ebGroupPurchaseShop.setEbGroupPurchaseId(ebGroupPurchase.getId());
                ebGroupPurchaseShop.setShopId(ebGroupBuyShop.getId().intValue());
                ebGroupPurchaseShop.setShopName(ebGroupBuyShop.getShopName());
                groupPurchaseShopList.add(ebGroupPurchaseShop);
            }
            request.setGroupPurchaseShopList(groupPurchaseShopList);
        }
        int i = 0;
        if (CollectionUtils.isNotEmpty(request.getGroupPurchaseShopList())) {
            //团购可用店铺
            for (EbGroupPurchaseShop ebGroupPurchaseShop : request.getGroupPurchaseShopList()) {
                ebGroupPurchaseShop.setEbGroupPurchaseId(ebGroupPurchase.getId());
                if (i != 0) {
                    sbf.append(",");
                }
                if (StringUtils.isNotBlank(ebGroupPurchaseShop.getShopName())) {
                    sbf.append(ebGroupPurchaseShop.getShopName());
                    i = i + 1;
                }

                ebGroupPurchaseShopDao.insert(ebGroupPurchaseShop);
            }
        }
        ebGroupPurchaseDao.update(ebGroupPurchase, new LambdaUpdateWrapper<EbGroupPurchase>().eq(EbGroupPurchase::getId, ebGroupPurchase.getId()).set(EbGroupPurchase::getShopNames, sbf.toString()));

        return ebGroupPurchase;
    }

    /**
     * 删除团购
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        log.info("delete:{}", id);
        return removeById(id);
    }

    /**
     * 修改团购
     *
     * @return
     */
    @Override
    public EbGroupPurchase update(EbGroupPurchaseRequest request) {
        log.info("update:{}", request.toString());
        Date date = new Date();
        EbGroupPurchase ebGroupPurchase = new EbGroupPurchase();
        BeanUtils.copyProperties(request, ebGroupPurchase);
        ebGroupPurchase.setUpdateTime(date);
        if (request.getBannerImageList() != null) {
            ebGroupPurchase.setBannerImages(ListUtils.getListStr(request.getBannerImageList()));
        }
        if (request.getDetailImageList() != null) {
            ebGroupPurchase.setDetailImages(ListUtils.getListStr(request.getDetailImageList()));
        }
        EbGroupPurchase groupPurchase = ebGroupPurchaseDao.selectById(request.getId());

        if (groupPurchase.isEbGroupPurchaseEqual(ebGroupPurchase)) {
            log.info("数据相同");
            ebGroupPurchase.setCheckStatus(1);
        } else {
            log.info("数据不一致");
            ebGroupPurchase.setCheckStatus(0);
        }
        ebGroupPurchaseDao.updateById(ebGroupPurchase);
        if (CollectionUtils.isNotEmpty(request.getGroupPurchaseBusinessHoursList())) {
            ebGroupPurchaseBusinessHoursDao.delete(new LambdaUpdateWrapper<EbGroupPurchaseBusinessHours>().eq(EbGroupPurchaseBusinessHours::getEbGroupPurchaseId, ebGroupPurchase.getId()));
            //团购可用时间
            for (EbGroupPurchaseBusinessHours hours : request.getGroupPurchaseBusinessHoursList()) {
                hours.setEbGroupPurchaseId(ebGroupPurchase.getId());
                ebGroupPurchaseBusinessHoursDao.insert(hours);
            }
        }

        if (CollectionUtils.isNotEmpty(request.getGoodsList())) {
            //团购商品
            ebGroupPurchaseGoodsDao.delete(new LambdaUpdateWrapper<EbGroupPurchaseGoods>().eq(EbGroupPurchaseGoods::getEbGroupPurchaseId, ebGroupPurchase.getId()));
            for (EbGroupPurchaseGoods ebGroupPurchaseGoods : request.getGoodsList()) {
                ebGroupPurchaseGoods.setEbGroupPurchaseId(ebGroupPurchase.getId());
                ebGroupPurchaseGoodsDao.insert(ebGroupPurchaseGoods);
            }
        }

        StringBuffer sbf = new StringBuffer();
        if (request.getIsAllShop() != null && request.getIsAllShop() == 1) {
            List<EbGroupBuyShop> merShopList = ebGroupBuyShopDao.selectList(new LambdaQueryWrapper<EbGroupBuyShop>().eq(EbGroupBuyShop::getMerId, request.getMerId()));
            List<EbGroupPurchaseShop> groupPurchaseShopList = new ArrayList<>();
            for (EbGroupBuyShop ebGroupBuyShop : merShopList) {
                EbGroupPurchaseShop ebGroupPurchaseShop = new EbGroupPurchaseShop();
                ebGroupPurchaseShop.setEbGroupPurchaseId(ebGroupPurchase.getId());
                ebGroupPurchaseShop.setShopId(ebGroupBuyShop.getId().intValue());
                ebGroupPurchaseShop.setShopName(ebGroupBuyShop.getShopName());
                groupPurchaseShopList.add(ebGroupPurchaseShop);
            }
            request.setGroupPurchaseShopList(groupPurchaseShopList);
        }
        int i = 0;
        if (CollectionUtils.isNotEmpty(request.getGroupPurchaseShopList())) {
            ebGroupPurchaseShopDao.delete(new LambdaUpdateWrapper<EbGroupPurchaseShop>().eq(EbGroupPurchaseShop::getEbGroupPurchaseId, ebGroupPurchase.getId()));
            //团购可用店铺
            for (EbGroupPurchaseShop ebGroupPurchaseShop : request.getGroupPurchaseShopList()) {
                ebGroupPurchaseShop.setEbGroupPurchaseId(ebGroupPurchase.getId());
                if (i != 0) {
                    sbf.append(",");
                }
                if (StringUtils.isNotBlank(ebGroupPurchaseShop.getShopName())) {
                    sbf.append(ebGroupPurchaseShop.getShopName());
                    i = i + 1;
                }
                ebGroupPurchaseShopDao.insert(ebGroupPurchaseShop);
            }
        }
        ebGroupPurchaseDao.update(ebGroupPurchase, new LambdaUpdateWrapper<EbGroupPurchase>().eq(EbGroupPurchase::getId, ebGroupPurchase.getId()).set(EbGroupPurchase::getShopNames, sbf.toString()));
        return ebGroupPurchaseDao.selectById(ebGroupPurchase.getId());
    }

    /**
     * 修改团购
     *
     * @param ebGroupPurchaseRequest
     * @return
     */
    @Override
    public EbGroupPurchase check(EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        log.info("check:{}", ebGroupPurchaseRequest.toString());
        if (ebGroupPurchaseRequest.getId() == null) {
            throw new IllegalArgumentException("ID不能为空");
        }
        ebGroupPurchaseDao.update(null, new LambdaUpdateWrapper<EbGroupPurchase>().set(EbGroupPurchase::getRemarks, ebGroupPurchaseRequest.getRemarks()).set(EbGroupPurchase::getCheckStatus, ebGroupPurchaseRequest.getCheckStatus()).set(EbGroupPurchase::getUpdateTime, new Date()).eq(EbGroupPurchase::getId, ebGroupPurchaseRequest.getId()));

        return ebGroupPurchaseDao.selectById(ebGroupPurchaseRequest.getId());
    }

    /**
     * 修改团购排序
     *
     * @param ebGroupPurchaseRequest
     * @return
     */
    @Override
    public void updateSortNum(EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        log.info("updateSortNum:{}", ebGroupPurchaseRequest.toString());
        if (ebGroupPurchaseRequest.getId() == null) {
            throw new IllegalArgumentException("ID不能为空");
        }
        updateSortNum(ebGroupPurchaseRequest.getId(), ebGroupPurchaseRequest.getSortNum());

    }

    /**
     * 修改团购排序
     *
     * @return
     */
    public void updateSortNum(Integer id, Integer sortNum) {
        log.info("updateSortNum:{}", id, sortNum);
        ebGroupPurchaseDao.update(null, new LambdaUpdateWrapper<EbGroupPurchase>().set(EbGroupPurchase::getSortNum, sortNum).set(EbGroupPurchase::getUpdateTime, new Date()).eq(EbGroupPurchase::getId, id));

    }

    /**
     * 修改团购状态
     *
     * @param ebGroupPurchaseRequest
     * @return
     */
    @Override
    public void updateStatus(EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        log.info("updateStatus:{}", ebGroupPurchaseRequest.toString());
        if (ebGroupPurchaseRequest.getId() == null || ebGroupPurchaseRequest.getStatus() == null) {
            throw new IllegalArgumentException("ID||status 不能为空");
        }
        long date = new Date().getTime();
        if (!ebGroupPurchaseRequest.getStatus() && ebGroupPurchaseRequest.getStartTime() != null) {
            if (date > ebGroupPurchaseRequest.getStartTime().getTime() && date < ebGroupPurchaseRequest.getEndTime().getTime()) {
                //在营业时间
                throw new IllegalArgumentException("下架失败,商品处于团购时间，请修改团购时间");
            }
        }
        ebGroupPurchaseDao.update(null, new LambdaUpdateWrapper<EbGroupPurchase>()
                .set(ebGroupPurchaseRequest.getEndTime() != null, EbGroupPurchase::getEndTime, ebGroupPurchaseRequest.getEndTime())
                .set(ebGroupPurchaseRequest.getStartTime() != null, EbGroupPurchase::getStartTime, ebGroupPurchaseRequest.getStartTime())
                .set(EbGroupPurchase::getStatus, ebGroupPurchaseRequest.getStatus()).set(EbGroupPurchase::getUpdateTime, new Date()).eq(EbGroupPurchase::getId, ebGroupPurchaseRequest.getId()));

    }

    /**
     * 修改团购状态
     *
     * @param ebGroupPurchaseRequest
     * @return
     */
    @Override
    public void updateDel(EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        log.info("updateStatus:{}", ebGroupPurchaseRequest.toString());
        if (ebGroupPurchaseRequest.getId() == null) {
            throw new IllegalArgumentException("ID不能为空");
        }
        ebGroupPurchaseDao.update(null, new LambdaUpdateWrapper<EbGroupPurchase>().set(EbGroupPurchase::getIsDelete, 1).set(EbGroupPurchase::getUpdateTime, new Date()).eq(EbGroupPurchase::getId, ebGroupPurchaseRequest.getId()));

    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Override
    public EbGroupPurchaseResponse getById(Long id) {
        log.info("getById:{}", id);
        EbGroupPurchaseResponse ebGroupPurchaseResponse = new EbGroupPurchaseResponse();
        EbGroupPurchase ebGroupPurchase = ebGroupPurchaseDao.selectById(id);
        if (ebGroupPurchase == null) {
            throw new CrmebException(CommonResultCode.VALIDATE_FAILED, "商品不存在");
        }
        BeanUtils.copyProperties(ebGroupPurchase, ebGroupPurchaseResponse);
        getInfo(ebGroupPurchase, ebGroupPurchaseResponse);
        return ebGroupPurchaseResponse;
    }

    /**
     * 查询列表
     *
     * @return
     */
    @Override
    public List<EbGroupPurchaseResponse> getList(EbGroupPurchaseRequest ebGroupPurchaseRequest) {

        List<Integer> idList = null;
        if (StringUtils.isNotBlank(ebGroupPurchaseRequest.getShopName())) {

            idList = ebGroupPurchaseShopDao.selectList(new LambdaQueryWrapper<EbGroupPurchaseShop>().select(EbGroupPurchaseShop::getEbGroupPurchaseId).like(EbGroupPurchaseShop::getShopName, ebGroupPurchaseRequest.getShopName())).stream().map(EbGroupPurchaseShop::getEbGroupPurchaseId).collect(Collectors.toList());
            if (CollectionUtil.isEmpty(idList)) {
                //无搜索内容
                idList = new ArrayList<>();
                idList.add(-1);
            }
        }

        if (ebGroupPurchaseRequest.getShopId() != null) {
            //根据店铺筛选
            idList = ebGroupPurchaseShopDao.selectList(new LambdaQueryWrapper<EbGroupPurchaseShop>().select(EbGroupPurchaseShop::getEbGroupPurchaseId).like(EbGroupPurchaseShop::getId, ebGroupPurchaseRequest.getShopId())).stream().map(EbGroupPurchaseShop::getEbGroupPurchaseId).collect(Collectors.toList());
            if (CollectionUtil.isEmpty(idList)) {
                //无搜索内容
                idList = new ArrayList<>();
                idList.add(-1);
            }
        }
        List<EbGroupPurchase> list = ebGroupPurchaseDao.selectList(new LambdaQueryWrapper<EbGroupPurchase>().eq(EbGroupPurchase::getIsDelete, 0).eq(ebGroupPurchaseRequest.getMerId() != null, EbGroupPurchase::getMerId, ebGroupPurchaseRequest.getMerId()).eq(ebGroupPurchaseRequest.getCheckStatus() != null, EbGroupPurchase::getCheckStatus, ebGroupPurchaseRequest.getCheckStatus()).eq(ebGroupPurchaseRequest.getStatus() != null, EbGroupPurchase::getStatus, Boolean.TRUE.equals(ebGroupPurchaseRequest.getStatus()) ? 1 : 0).eq(ebGroupPurchaseRequest.getCategoryId() != null, EbGroupPurchase::getCategoryId, ebGroupPurchaseRequest.getCategoryId()).eq(ebGroupPurchaseRequest.getMerName() != null, EbGroupPurchase::getMerName, ebGroupPurchaseRequest.getMerName()).eq(ebGroupPurchaseRequest.getIsStock() != null && ebGroupPurchaseRequest.getIsStock() == 1, EbGroupPurchase::getCheckStatus, 1).apply(ebGroupPurchaseRequest.getIsStock() != null && ebGroupPurchaseRequest.getIsStock() == 1, " stock_warning >= stock").like(ebGroupPurchaseRequest.getName() != null, EbGroupPurchase::getName, ebGroupPurchaseRequest.getName()).in(idList != null, EbGroupPurchase::getId, idList)
                // 使用 ge 来替代手动拼接 SQL
                .between(ebGroupPurchaseRequest.getStartTime() != null, EbGroupPurchase::getCreateTime, ebGroupPurchaseRequest.getStartTime(), ebGroupPurchaseRequest.getEndTime()).orderByDesc(EbGroupPurchase::getSortNum));
        List<EbGroupPurchaseResponse> responseList = new ArrayList<>();
        for (EbGroupPurchase ebGroupPurchase : list) {
            EbGroupPurchaseResponse ebGroupPurchaseResponse = new EbGroupPurchaseResponse();
            BeanUtils.copyProperties(ebGroupPurchase, ebGroupPurchaseResponse);
            getInfo(ebGroupPurchase, ebGroupPurchaseResponse);
            responseList.add(ebGroupPurchaseResponse);
        }
        return responseList;
    }

    /**
     * 查询列表
     *
     * @return
     */

    public List<EbGroupPurchaseResponse> getPlatformList(EbGroupPurchaseRequest ebGroupPurchaseRequest) {

        List<Integer> idList = null;
        if (StringUtils.isNotBlank(ebGroupPurchaseRequest.getShopName())) {

            idList = ebGroupPurchaseShopDao.selectList(new LambdaQueryWrapper<EbGroupPurchaseShop>().select(EbGroupPurchaseShop::getEbGroupPurchaseId).like(EbGroupPurchaseShop::getShopName, ebGroupPurchaseRequest.getShopName())).stream().map(EbGroupPurchaseShop::getEbGroupPurchaseId).collect(Collectors.toList());
            if (CollectionUtil.isEmpty(idList)) {
                //无搜索内容
                idList = new ArrayList<>();
                idList.add(-1);
            }
        }

        if (ebGroupPurchaseRequest.getShopId() != null) {
            //根据店铺筛选
            idList = ebGroupPurchaseShopDao.selectList(new LambdaQueryWrapper<EbGroupPurchaseShop>().select(EbGroupPurchaseShop::getEbGroupPurchaseId).like(EbGroupPurchaseShop::getId, ebGroupPurchaseRequest.getShopId())).stream().map(EbGroupPurchaseShop::getEbGroupPurchaseId).collect(Collectors.toList());
            if (CollectionUtil.isEmpty(idList)) {
                //无搜索内容
                idList = new ArrayList<>();
                idList.add(-1);
            }
        }
        List<EbGroupPurchase> list = ebGroupPurchaseDao.selectList(new LambdaQueryWrapper<EbGroupPurchase>().eq(EbGroupPurchase::getIsDelete, 0).eq(ebGroupPurchaseRequest.getMerId() != null, EbGroupPurchase::getMerId, ebGroupPurchaseRequest.getMerId()).eq(ebGroupPurchaseRequest.getCheckStatus() != null, EbGroupPurchase::getCheckStatus, ebGroupPurchaseRequest.getCheckStatus()).eq(ebGroupPurchaseRequest.getStatus() != null, EbGroupPurchase::getStatus, Boolean.TRUE.equals(ebGroupPurchaseRequest.getStatus()) ? 1 : 0).eq(ebGroupPurchaseRequest.getCategoryId() != null, EbGroupPurchase::getCategoryId, ebGroupPurchaseRequest.getCategoryId()).eq(ebGroupPurchaseRequest.getMerName() != null, EbGroupPurchase::getMerName, ebGroupPurchaseRequest.getMerName()).eq(ebGroupPurchaseRequest.getIsStock() != null && ebGroupPurchaseRequest.getIsStock() == 1, EbGroupPurchase::getCheckStatus, 1).apply(ebGroupPurchaseRequest.getIsStock() != null && ebGroupPurchaseRequest.getIsStock() == 1, " stock_warning >= stock").like(ebGroupPurchaseRequest.getName() != null, EbGroupPurchase::getName, ebGroupPurchaseRequest.getName()).in(idList != null, EbGroupPurchase::getId, idList)
                // 使用 ge 来替代手动拼接 SQL
                .between(ebGroupPurchaseRequest.getStartTime() != null, EbGroupPurchase::getCreateTime, ebGroupPurchaseRequest.getStartTime(), ebGroupPurchaseRequest.getEndTime()).orderByDesc(EbGroupPurchase::getCreateTime));
        List<EbGroupPurchaseResponse> responseList = new ArrayList<>();
        for (EbGroupPurchase ebGroupPurchase : list) {
            EbGroupPurchaseResponse ebGroupPurchaseResponse = new EbGroupPurchaseResponse();
            BeanUtils.copyProperties(ebGroupPurchase, ebGroupPurchaseResponse);
            getInfo(ebGroupPurchase, ebGroupPurchaseResponse);
            responseList.add(ebGroupPurchaseResponse);
        }
        return responseList;
    }

    /**
     * 构建基本信息
     *
     * @param ebGroupPurchase
     * @param ebGroupPurchaseResponse
     */
    private void getInfo(EbGroupPurchase ebGroupPurchase, EbGroupPurchaseResponse ebGroupPurchaseResponse) {
        ebGroupPurchaseResponse.setBannerImageList(getStrList(ebGroupPurchase.getBannerImages()));
        ebGroupPurchaseResponse.setDetailImageList(getStrList(ebGroupPurchase.getDetailImages()));

        ebGroupPurchaseResponse.setGroupPurchaseBusinessHoursList(ebGroupPurchaseBusinessHoursDao.selectList(new LambdaQueryWrapper<EbGroupPurchaseBusinessHours>().eq(EbGroupPurchaseBusinessHours::getEbGroupPurchaseId, ebGroupPurchase.getId())));
        ebGroupPurchaseResponse.setGoodsList(ebGroupPurchaseGoodsDao.selectList(new LambdaQueryWrapper<EbGroupPurchaseGoods>().eq(EbGroupPurchaseGoods::getEbGroupPurchaseId, ebGroupPurchase.getId())));
        ebGroupPurchaseResponse.setGroupPurchaseShopList(ebGroupPurchaseShopDao.selectList(new LambdaQueryWrapper<EbGroupPurchaseShop>().eq(EbGroupPurchaseShop::getEbGroupPurchaseId, ebGroupPurchase.getId())));
        EbIndustryCategory ebIndustryCategory = ebIndustryCategoryService.getById(ebGroupPurchase.getCategoryId());
        if (ebIndustryCategory != null) {
            ebGroupPurchaseResponse.setCategoryName(ebIndustryCategory.getName());
        }
        long date = new Date().getTime();

        if (date > ebGroupPurchaseResponse.getStartTime().getTime() && date < ebGroupPurchaseResponse.getEndTime().getTime()) {
            //自动上架
            ebGroupPurchaseResponse.setStatus(true);
        } else {
            ebGroupPurchaseResponse.setStatus(false);
        }
    }

    /**
     * 分页查询
     *
     * @return
     */
    @Override
    public PageInfo<EbGroupPurchaseResponse> getPage(EbGroupPurchaseRequest request) {
        log.info("getPage: page={}, size={}", request.getPage(), request.getLimit());
        Page<EbGroupPurchase> page = PageHelper.startPage(request.getPage(), request.getLimit());
        List<EbGroupPurchaseResponse> list = getList(request);
        return CommonPage.copyPageInfo(page, list);
    }

    /**
     * 分页查询
     *
     * @return
     */
    @Override
    public PageInfo<EbGroupPurchaseResponse> getPlatformPage(EbGroupPurchaseRequest request) {
        log.info("getPage: page={}, size={}", request.getPage(), request.getLimit());
        Page<EbGroupPurchase> page = PageHelper.startPage(request.getPage(), request.getLimit());
        List<EbGroupPurchaseResponse> list = getPlatformList(request);
        return CommonPage.copyPageInfo(page, list);
    }

    /**
     * 查询统计信息
     *
     * @return
     */
    @Override
    public EbGroupPurchaseStatisticsVo getShopStatistics(Integer mId) {

        return ebGroupPurchaseDao.getShopStatistics(mId);
    }

    @Override
    public void addStock(EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        log.info("addStock:{}", ebGroupPurchaseRequest.toString());
        if (ebGroupPurchaseRequest.getId() == null) {
            throw new IllegalArgumentException("ID不能为空");
        }
        EbGroupPurchase ebGroupPurchase = getById(ebGroupPurchaseRequest.getId());
        if (ebGroupPurchase.getStock() != null || ebGroupPurchase.getStock() < 0) {
            ebGroupPurchase.setStock(ebGroupPurchase.getStock() + ebGroupPurchaseRequest.getStock());
        } else {
            ebGroupPurchase.setStock(ebGroupPurchaseRequest.getStock());
        }
        ebGroupPurchaseDao.update(null, new LambdaUpdateWrapper<EbGroupPurchase>().set(EbGroupPurchase::getStock, ebGroupPurchase.getStock()).set(EbGroupPurchase::getUpdateTime, new Date()).eq(EbGroupPurchase::getId, ebGroupPurchaseRequest.getId()));
    }

    @Override
    public void addOrderStock(Integer id, Integer stock) {
        EbGroupPurchase ebGroupPurchase = getById(id);
        if (ebGroupPurchase != null && ebGroupPurchase.getStock() < 0) {
            ebGroupPurchase.setStock(ebGroupPurchase.getStock() + stock);
        }else{
            return;
        }
        ebGroupPurchaseDao.update(null, new LambdaUpdateWrapper<EbGroupPurchase>().set(EbGroupPurchase::getStock,
                ebGroupPurchase.getStock()).
                eq(EbGroupPurchase::getId, id));
    }

    /**
     * 扣库存
     *
     * @param id
     * @param stock
     */
    @Override
    public void delStock(Integer id, Integer stock) {
        EbGroupPurchase ebGroupPurchase = getById(id);
        if (ebGroupPurchase.getStock() < stock) {
            throw new IllegalArgumentException("库存不足");
        } else {
            ebGroupPurchase.setStock(ebGroupPurchase.getStock() - stock);
        }
        ebGroupPurchaseDao.update(null, new LambdaUpdateWrapper<EbGroupPurchase>().set(EbGroupPurchase::getStock, ebGroupPurchase.getStock()).eq(EbGroupPurchase::getId, id));
    }

    @Override
    public List<EbGroupPurchaseResponse> getShopList(Long id) {
        List<Integer> idList = null;
        //根据店铺筛选
        idList = ebGroupPurchaseShopDao.selectList(new LambdaQueryWrapper<EbGroupPurchaseShop>().select(EbGroupPurchaseShop::getEbGroupPurchaseId).in(EbGroupPurchaseShop::getShopId, id)).stream().map(EbGroupPurchaseShop::getEbGroupPurchaseId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(idList)) {
            return new ArrayList<>();
        }
        List<EbGroupPurchase> list = ebGroupPurchaseDao.selectList(new LambdaQueryWrapper<EbGroupPurchase>().in(EbGroupPurchase::getId, idList).eq(EbGroupPurchase::getStatus, 1).orderByDesc(EbGroupPurchase::getSortNum));
        List<EbGroupPurchaseResponse> responseList = new ArrayList<>();
        for (EbGroupPurchase ebGroupPurchase : list) {
            EbGroupPurchaseResponse ebGroupPurchaseResponse = new EbGroupPurchaseResponse();
            BeanUtils.copyProperties(ebGroupPurchase, ebGroupPurchaseResponse);
            getInfo(ebGroupPurchase, ebGroupPurchaseResponse);

            responseList.add(ebGroupPurchaseResponse);
        }
        return responseList;
    }

    /**
     * 添加销量
     *
     * @param productId
     * @param payNum
     */
    @Override
    public void addSales(Integer productId, Integer payNum) {
        EbGroupPurchase ebGroupPurchase = ebGroupPurchaseDao.selectById(productId);
        ebGroupPurchaseDao.update(null, new LambdaUpdateWrapper<EbGroupPurchase>().set(EbGroupPurchase::getSalesVolume, ebGroupPurchase.getSalesVolume() + payNum).set(EbGroupPurchase::getPayNum, ebGroupPurchase.getPayNum() + 1).eq(EbGroupPurchase::getId, productId));
    }

    /**
     * 定时修改商品状态
     */
    @Override
    public void setStatusTask() {
        List<EbGroupPurchase> list = ebGroupPurchaseDao.selectList(new LambdaQueryWrapper<EbGroupPurchase>().eq(EbGroupPurchase::getCheckStatus, 1));
        long date = new Date().getTime();
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(item -> {
                item.setStatus(false);
                if (date > item.getStartTime().getTime() && date < item.getEndTime().getTime()) {
                    //自动上架
                    item.setStatus(true);
                } else {
                    item.setStatus(false);
                }
                ebGroupPurchaseDao.updateById(item);
            });
        }
    }

    private List<String> getStrList(String strs) {
        List<String> idList = new ArrayList<>();
        if (strs != null && !strs.isEmpty()) {
            idList = Arrays.stream(strs.split(",")).map(String::valueOf).collect(Collectors.toList());
        }
        return idList;
    }
}
